<?php

/**
 * This is the model class for table "{{express}}".
 *
 * The followings are the available columns in table '{{express}}':
 * @property integer $ze_id
 * @property integer $zm_id
 * @property string $ze_title
 * @property integer $ze_valuation
 * @property double $ze_freeprice
 * @property string $ze_addr
 * @property string $ze_content
 */
class Express extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Express the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{express}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, ze_title, ze_addr,zar_id', 'required'),
			array('zm_id, ze_valuation,zar_id', 'numerical', 'integerOnly'=>true),
			array('ze_freeprice', 'numerical'),
			array('ze_title', 'length', 'max'=>100),
			array('ze_addr', 'length', 'max'=>255),
			array('ze_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ze_id, zm_id, ze_title, zar_id, ze_valuation, ze_freeprice, ze_addr, ze_content', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'logs'=>array(self::HAS_MANY, 'ExpressLog', 'ze_id'),
				'default'=>array(self::HAS_MANY, 'ExpressLog', 'ze_id','on'=>'default.zel_area=""'),//默认地区物流价格
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ze_id' => 'Ze',
			'zm_id' => 'Zm',
			'ze_title' => 'Ze Title',
			'ze_valuation' => 'Ze Valuation',
			'ze_freeprice' => 'Ze Freeprice',
			'ze_addr' => 'Ze Addr',
			'zar_id'  =>'省市ID',
			'ze_content' => 'Ze Content',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ze_id',$this->ze_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('ze_title',$this->ze_title,true);
		$criteria->compare('ze_valuation',$this->ze_valuation);
		$criteria->compare('ze_freeprice',$this->ze_freeprice);
		$criteria->compare('ze_addr',$this->ze_addr,true);
		$criteria->compare('zar_id',$this->zar_id,true);
		$criteria->compare('ze_content',$this->ze_content,true);
		$criteria->order = 'ze_id DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>'3'),
		));
	}
	/**
	 * 获取当前会员 的运费模板
	 * */
	public function getPressByActiveUser(){
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$express = $this->findAll($criteria);
		return CHtml::listData($express,'ze_id','ze_title');
	}
	
	/**
	 * 物流公司
	 * */
	public function getExpressCom($type=null){
		if($type==null){
			$criteria=new CDbCriteria;
			$express = ExpressCom::model()->findAll($criteria);
			return CHtml::listData($express,'zec_id','zec_title');
		}else{
			$level = $this->getExpressCom();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 计价方式
	 * */
	public function getPricing($type=null){
		if($type==null){
			return array(
					'1'=>'按件数',
					'2'=>'按重量',
					'3'=>'按体积',
					);
		}else{
			$level = $this->getPricing();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 运送方式
	 */
	public function getFreightType($type=null){
		if($type==null){
			return array(
					'1'=>'快递',
					'2'=>'EMS',
					'3'=>'平邮',
			);
		}else{
			$level = $this->getFreightType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	/**
	 * @获取计量方式
	 */
	public function getMeteringType($type=null){
		if($type==null){
			return array(
					'1'=>'件（件）',
					'2'=>'重（kg）',
					'3'=>'体积（m<sup>3</sup>）',
			);
		}else{
			$level = $this->getMeteringType();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}

}